package com.java1412.interceptor;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.java1412.utils.JWTUtil;
import com.java1412.utils.ResponseUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截用户是否处于登录状态
 *
 * @author java1412
 * @since 2023-08-07 15:41:06
 */
@Component
public class AdminLoginInterceptor implements HandlerInterceptor {
    // 请求到控制器之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取token,token是放在请求头携带过来
        String token = request.getHeader("token");
        System.out.println(token);
//        if (null == token || "".equals(token)) {
//            // 没有携带token
//            // 返回json给前端
//            ResponseUtil r = ResponseUtil.fail("没有携带token，无法访问后续接口");
//            response.setContentType("application/json;charset=UTF-8");
//            // om对象可以将Object对象转成json
//            ObjectMapper om = new ObjectMapper();
//            String json = om.writeValueAsString(r);
//            response.getWriter().write(json);
//            // 不能放行
//            return false;
//        } else {
//            // 有token，直接放行
//            return true;
//        }
        ResponseUtil r = null;
        try {
            DecodedJWT decodedJWT = JWTUtil.verify(token);
            // r = ResponseUtil.ok("请求成功");
            return true; // 放行
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            r = ResponseUtil.fail("无效签名");
        } catch (TokenExpiredException e) {
            e.printStackTrace();
            r = ResponseUtil.fail("token过期");
        } catch (AlgorithmMismatchException e) {
            e.printStackTrace();
            r = ResponseUtil.fail("算法不一致");
        } catch (Exception e) {
            e.printStackTrace();
            r = ResponseUtil.fail("token无效");
        }
        response.setContentType("application/json;charset=UTF-8");
        // om对象可以将Object对象转成json
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(r);
        response.getWriter().write(json);
        // 请求失败，需要将数据以json返回
        return false; // 不放行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
